package com.jiantong.web.open.app.lawEnforcing;

import com.jiantong.lawEnforcing.model.Goods;
import com.jiantong.lawEnforcing.service.GoodsService;
import com.jiantong.web.core.enums.ExceptionCode;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * 物件信息
 * Created by mz03 on 2017/8/15.
 */
@Slf4j
@OpenBean
public class GoodsApis {
    @RpcConsumer
    private GoodsService goodsService;


    /**
     * 根据id查询一条信息
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.Goods.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public Goods findById(Long id){
        Response<Goods> response = goodsService.finById(id);
        if (!response.isSuccess()){
            log.error("failed to find Goods read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 根据idList 查找一组
     * @param ids
     * @return
     */
    @OpenMethod(
            key = "find.GoodsList.by.ids",
            paramNames = {"ids"},
            httpMethods = RequestMethod.GET
    )
    public List<Goods> findById(List<Long> ids){
        Response<List<Goods>> response = goodsService.finByIds(ids);
        if (!response.isSuccess()){
            log.error("failed to find GoodsList read by id = {}, cause : {}", ids, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 创建一条信息
     * @param goods
     * @return
     */
    @OpenMethod(
            key = "create.Goods",
            paramNames = {"Goods"},
            httpMethods = RequestMethod.POST
    )
    public Long create(Goods goods){
        Response<Long> response = goodsService.create(goods);
        if(!response.isSuccess()){
            log.error("failed to create goods read by goods = {}, cause : {}", goods, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 创建一条信息
     * @param goods
     * @return
     */
    @OpenMethod(
            key = "create.Goods.update.datas",
            paramNames = {"Goods","lawCase","writType","idList"},
            httpMethods = RequestMethod.POST
    )
    public Long createAndDatas(Goods goods,Long lawCase,Integer writType,String idList){
        Response<Long> response = goodsService.createAndDatas(goods,lawCase,writType,idList);
        if(!response.isSuccess()){
            log.error("failed to create goods read by goods = {}, cause : {}", goods, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 更新单据
     */
    @OpenMethod(
            key = "update.ask.goods",
            paramNames = {"goods"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean update(Goods goods){
        if(goods.getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Boolean> response = goodsService.update(goods);
        if(!response.isSuccess()){
            log.error("failed to update goods");
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(),
                    "failed to update goods");
        }
        return response.getResult();
    }

}
